package com.gistmap.web;

import com.gistmap.bean.Article;
import com.gistmap.bean.User;
import com.gistmap.dto.ActivityDto;
import com.gistmap.dto.ArticleDto;
import com.gistmap.dto.ArticleIndexDto;
import com.gistmap.dto.CommentDto;
import com.gistmap.service.ActivityService;
import com.gistmap.service.ArticleService;
import com.gistmap.service.CommentService;
import com.gistmap.service.UserService;
import com.gistmap.util.Constant;
import com.gistmap.util.Pager;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private ActivityService activityService;

    @RequestMapping(value="/writeArticle",method = RequestMethod.POST)
    public ModelAndView saveArticle(String title, String editor1, HttpServletRequest request)
    {
        ModelAndView mv = new ModelAndView("tips");

        User user = (User)request.getSession().getAttribute("user");
        System.out.println(title);
        System.out.println(editor1);

        Article article = new Article();
        article.setTitle(title);
        article.setContent(editor1);
        article.setUid(user.getId());

        articleService.save(article);

        mv.addObject("tip", Constant.WRITE_ARTICLE_SUCCESS);


        return mv;
    }

    @RequestMapping("/articleInfo")
    public ModelAndView articleInfo(HttpServletRequest request)
    {
        User user = (User)request.getSession().getAttribute("user");
        ModelAndView mv = new ModelAndView("userIndex");

        Pager pager = null;

        int pageSize = 5;
        int pageNo = 0;

        String pageNoStr = request.getParameter("pageNo");
        System.out.println(pageNoStr);
        if (null == pageNoStr)
            pageNo = 1;
        else
            pageNo = Integer.parseInt(pageNoStr);

        Map<String,Object> params  =  new HashedMap();
        params.put("index", pageNo);
        params.put("pageSize", pageSize);
        params.put("uid",user.getId());

        pager = articleService.getByPage(params);
        User user1 = userService.getByUserName(user.getUsername());
        mv.addObject("user",user1);
        mv.addObject("Pager",pager);
        return mv;
    }

    @RequestMapping("/getArticle")
    public ModelAndView getArticle(String aid){
        System.out.println(aid);
        ModelAndView mv = new ModelAndView("showArticle");
        ArticleDto articleDto = articleService.get(aid);

        List<CommentDto> replyList= commentService.getReplyList(aid);
        mv.addObject("ATD",articleDto);
        mv.addObject("replyList",replyList);
        return mv;
    }

    /*
        登录进来的首页数据（活动，文章）
     */
    @RequestMapping("/mainData")
    public ModelAndView mainData(HttpServletRequest request)
    {
        ModelAndView mv = new ModelAndView("main");

        List<ArticleIndexDto> artlist = articleService.randomMain();

        Map<String,Object> map = new HashedMap();
        map.put("index",0);
        map.put("size",5);
        List<ActivityDto> list = activityService.showAct(map);



        mv.addObject("artlist",artlist);
        mv.addObject("ActShow",list);
        return mv;
    }

    @RequestMapping("/allArticles")
    public ModelAndView allArticles(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("allArticles");
        Pager pager = null;

        int pageSize = 10;
        int pageNo = 0;

        String pageNoStr = request.getParameter("pageNo");
        System.out.println(pageNoStr);
        if (null == pageNoStr)
            pageNo = 1;
        else
            pageNo = Integer.parseInt(pageNoStr);

        Map<String,Object> params  =  new HashedMap();
        params.put("index", pageNo);
        params.put("pageSize", pageSize);

        pager = articleService.randomGet(params);


        mv.addObject("PagerMain",pager);
        return mv;
    }
}
